package com.intelligentmeterreadingsys.otherequipment.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.intelligentmeterreadingsys.BaseDao.BaseDao;
import com.intelligentmeterreadingsys.povo.OtherEquipmentPOVO;
import com.intelligentmeterreadingsys.povo.TypePoVo;
import com.intelligentmeterreadingsys.util.DateUtil;
import com.intelligentmeterreadingsys.util.Model;

import dbhelper.DbTableName;

public class EquipementDao extends BaseDao{
    private SQLiteDatabase db;
	public EquipementDao(Context context) {
		super(context);
		this.db=super.getDb();
		
	}

	/**
	 * 查询设备类型
	 * 
	 * @return
	 */
	public List<TypePoVo> queryOrderType() {
		List<TypePoVo> list = new ArrayList<TypePoVo>();
		String sql = "select * from type where status='2'";
		Cursor cursor = db.rawQuery(sql, null);
      
		if (cursor == null) {
			
			return null;
		}
		while (cursor.moveToNext()) {
			TypePoVo type = new TypePoVo();
			
			type.setName(cursor.getString(cursor.getColumnIndex("name")));
			System.out.println(type.getName()+"------------");
			type.setNumber(cursor.getString(cursor.getColumnIndex("number")));
			
			list.add(type);
		}
		
		if(cursor!=null){
			cursor.close();
		}
		return list;
	}

	/**
	 * 添加其它设备
	 * @param equi
	 * @return
	 */
	public String insertEquipement(OtherEquipmentPOVO equi){
		String str="0";
		if(equi==null){
			return str;
		}
		if(equi.getNumber()==null||equi.getNumber().equals("")){
			return str;
		}
		ContentValues values=new ContentValues();
		values.put("ep_number", equi.getNumber());
		values.put("ep_typename", equi.getTypename());
		values.put("ep_type", equi.getTypeid());
		values.put("ep_adds", equi.getAdds());
		values.put("ep_locationadds", equi.getLocationadds());
		values.put("latitude", equi.getLatitude());
		values.put("longitude", equi.getLongitude());
		values.put("ep_date", DateUtil.getBeforeDay(0));
		values.put("isadd", "新增");
		values.put("isupload", "0");
		values.put("ep_dess", equi.getDesc());
		values.put("RFIDnumber", equi.getRfidNumber());
		long id=db.insert(DbTableName.EQUIPEMENT, null, values);
	
		if(id>0){
		
			String sql="select max(_id) as max_id from "+DbTableName.EQUIPEMENT;
			Cursor cursor=db.rawQuery(sql, null);
			if(cursor.moveToNext()){
				String otherid=cursor.getString(cursor.getColumnIndex("max_id"));
				str=otherid;
				List<ContentValues> list=new ArrayList<ContentValues>();
				if(equi.getList()!=null&&equi.getList().size()>0){
					for(int i=0;i<equi.getList().size();i++){
						ContentValues value=new ContentValues();
						value.put("status", "4");
						value.put("type", "1");
						value.put("path", equi.getList().get(i));
						value.put("otherid", otherid);
						list.add(value);
						
					}
					super.insertALL(list, DbTableName.EQUIPEMENT);
				}
				
				
			}
			if(cursor!=null){
				cursor.close();
			}
		}
		return str;
	}
	
	
	/**
	 * 查询上传其它设备信息
	 * @param id
	 * @return
	 */
	public List<OtherEquipmentPOVO> queryUploadEquipment(String id,String isadd,String isupload,String checkstatus){
		
		StringBuffer sb=new StringBuffer("select * from otherequipment o where 0=0");
		if(id!=null){
			sb.append(" and o.[_id]=").append(id);
		}
		if(isadd!=null){
			sb.append(" and o.[isadd]='").append(isadd).append("'");
		}
		if(isupload!=null&&isadd!=null){
			if(isadd.equals("巡检")){
				sb.append(" and o.[checkupload]='").append(isupload).append("'");
			}else{
				sb.append(" and o.[isupload]='").append(isupload).append("'");
			}
			
		}
		if(checkstatus!=null){
		    sb.append(" and o.[checkstatus]='").append(checkstatus).append("'");
		}
		String sql=sb.toString();
		Cursor cursor=db.rawQuery(sql, null);
		List<OtherEquipmentPOVO> list=new ArrayList<OtherEquipmentPOVO>();
		while(cursor.moveToNext()){
			OtherEquipmentPOVO e=new OtherEquipmentPOVO();
			e.setAdds(cursor.getString(cursor.getColumnIndex("ep_adds")));
			e.setId(cursor.getString(cursor.getColumnIndex("_id")));
			e.setDate(cursor.getString(cursor.getColumnIndex("ep_date")));
			e.setDesc(cursor.getString(cursor.getColumnIndex("ep_dess")));
			e.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
			e.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
			e.setLocationadds(cursor.getString(cursor.getColumnIndex("ep_locationadds")));
			e.setNumber(cursor.getString(cursor.getColumnIndex("ep_number")));
			e.setTypeid(cursor.getString(cursor.getColumnIndex("ep_type")));
			e.setTypename(cursor.getString(cursor.getColumnIndex("ep_typename")));
		    e.setCheckdate(cursor.getString(cursor.getColumnIndex("ep_checkdate")));
		    e.setCheckdesc(cursor.getString(cursor.getColumnIndex("ep_checkdesc")));
		    e.setChecktype(cursor.getString(cursor.getColumnIndex("ep_checktype")));
		    e.setRfidNumber(cursor.getString(cursor.getColumnIndex("RFIDnumber")));
		    e.setIsnorfid(cursor.getString(cursor.getColumnIndex("isnorfid")));
		    e.setIsupload(cursor.getString(cursor.getColumnIndex("isupload")));
		    String ImageSql=null;
			ImageSql="select * from "+DbTableName.RESOURCES+" where type='1' and status='4' and otherid="+e.getId();
			if(isadd.equals("巡检")||isadd.equals("修改")){
			ImageSql="select * from "+DbTableName.RESOURCES+" where type='1' and status='5' and otherid="+e.getId();
			}
			Cursor c=db.rawQuery(ImageSql, null);
			 List<String> imgpath=new ArrayList<String>();
			while(c.moveToNext()){
			   imgpath.add(c.getString(c.getColumnIndex("path")));
			}
			if(c!=null){
				c.close();
			}
			e.setList(imgpath);
			list.add(e);
		}
		if(cursor!=null){
			cursor.close();
		}
		return list;
		
	}
	
	/**
	 * 查询设备信息(分页)
	 * @param isadds
	 * @param serchstatus
	 * @param querystatus
	 * @param number
	 * @param adds
	 * @return
	 */
	public List<OtherEquipmentPOVO> queryEquipment(String typename,String serchstatus,String querystatus,
			                          String number,String adds,Model model){
		StringBuffer sb=new StringBuffer("select * from otherequipment o where 0=0");
		
		if(querystatus!=null){
			
			if(querystatus.equals("巡检")){
				if(serchstatus!=null){
				    sb.append(" and o.[checkstatus]='").append("已检查").append("' or o.[checkstatus]='")
				    .append("未检查'");
				}
			}else{
				sb.append(" and o.[isadd]='").append(querystatus).append("'");
			}
		}
		if(number!=null&&!number.equals("")){
			sb.append(" and o.[ep_number]='").append(number).append("'");
		}
		if(adds!=null&&!adds.equals("")){
			sb.append(" and o.[ep_adds] like'%").append(adds).append("%'");
		}
		String sql=sb.toString();
		Cursor cursor=super.getAllItems((model.getIndex()-1)*model.getView_Count(), model.getView_Count(), sql);
		List<OtherEquipmentPOVO> list=new ArrayList<OtherEquipmentPOVO>();
		while(cursor.moveToNext()){
			OtherEquipmentPOVO e=new OtherEquipmentPOVO();
			e.setAdds(cursor.getString(cursor.getColumnIndex("ep_adds")));
			e.setId(cursor.getString(cursor.getColumnIndex("_id")));
			e.setDate(cursor.getString(cursor.getColumnIndex("ep_date")));
			e.setDesc(cursor.getString(cursor.getColumnIndex("ep_dess")));
			e.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
			e.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
			e.setLocationadds(cursor.getString(cursor.getColumnIndex("ep_locationadds")));
			e.setNumber(cursor.getString(cursor.getColumnIndex("ep_number")));
			e.setTypeid(cursor.getString(cursor.getColumnIndex("ep_type")));
			e.setTypename(cursor.getString(cursor.getColumnIndex("ep_typename")));
			e.setIsadd(cursor.getString(cursor.getColumnIndex("isadd")));
			e.setRfidNumber(cursor.getString(cursor.getColumnIndex("RFIDnumber")));
			e.setIsupload(cursor.getString(cursor.getColumnIndex("isupload")));
			list.add(e);
	
		
	     }
		if(cursor!=null){
			cursor.close();
		}
		return list;
	}	
	
	/**
	 * 设备查询分页总数
	 * @param typename
	 * @param serchstatus
	 * @param querystatus
	 * @param number
	 * @param adds
	 * @param model
	 * @return
	 */
	public int queryTotalPage(String typename,String serchstatus,String querystatus,
			                          String number,String adds,Model model){
			int i=0;
			int page=0;
			StringBuffer sb=new StringBuffer("select * from otherequipment o where 0=0");
			
			if(querystatus!=null){
				
				if(querystatus.equals("巡检")){
					if(serchstatus!=null){
					    sb.append(" and o.[checkstatus]='").append("已检查").append("' or o.checkstatus='")
					    .append("未检查'");
					}
				}else{
					sb.append(" and o.[isadd]='").append(querystatus).append("'");
				}
			}
			if(number!=null&&!number.equals("")){
				sb.append(" and o.[ep_number]='").append(number).append("'");
			}
			if(adds!=null&&!adds.equals("")){
				sb.append(" and o.[ep_adds] like'%").append(adds).append("%'");
			}
			String sql=sb.toString();
			i=super.getCount(sql);
			 page=i%10>0?i/10+1:i/10;
			return page;
		
	}
	
	/**
	 * 通过RFID 或ID 查询设备信息
	 * @param id
	 * @param rfid
	 * @return
	 */
	public OtherEquipmentPOVO queryOne(String id,String rfid){
		OtherEquipmentPOVO e=null;
		String sql="select * from otherequipment o where 0=0 ";
		if(id!=null){
			sql=sql+" and o.[_id]="+id;
		}
		if(rfid!=null&&rfid.equals("")){
			sql=sql+" and o.[RfidNumber]="+rfid;
		}
		Cursor cursor=db.rawQuery(sql, null);
		if(cursor.moveToNext()){
			e=new OtherEquipmentPOVO();
			e.setAdds(cursor.getString(cursor.getColumnIndex("ep_adds")));
			e.setId(cursor.getString(cursor.getColumnIndex("_id")));
			e.setDate(cursor.getString(cursor.getColumnIndex("ep_date")));
			e.setDesc(cursor.getString(cursor.getColumnIndex("ep_dess")));
			e.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
			e.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
			e.setLocationadds(cursor.getString(cursor.getColumnIndex("ep_locationadds")));
			e.setNumber(cursor.getString(cursor.getColumnIndex("ep_number")));
			e.setTypeid(cursor.getString(cursor.getColumnIndex("ep_type")));
			e.setTypename(cursor.getString(cursor.getColumnIndex("ep_typename")));
			e.setIsadd(cursor.getString(cursor.getColumnIndex("isadd")));
			e.setRfidNumber(cursor.getString(cursor.getColumnIndex("RFIDnumber")));
			e.setIsupload(cursor.getString(cursor.getColumnIndex("isupload")));
			
		}
		if(cursor!=null){
			cursor.close();
		}
		return e;
	}
	
	/**
	 * 删除新增设备信息
	 * @param id
	 * @return
	 */
	   public boolean delectEquipement(String id){
		   boolean b=false;
		   int ids=0;
		   db.delete(DbTableName.EQUIPEMENT, "_id=?", new String[]{id});
		  ids= db.delete(DbTableName.RESOURCES, "otherid=?,status=?", new String[]{id,"4"});
		  if(ids>0){
			  b=true;
		  }
		return b;
		   
	   }
	   
	   /**
	    * 上传成功后修改状态
	    * @param id
	    * @return
	    */
	   public boolean modifyEquiPement(String id){
		   boolean b=false;
		   ContentValues values=new ContentValues();
		   values.put("isupload", "1");
		 int i=  db.update(DbTableName.EQUIPEMENT, values, "_id=?", new String[]{id});
		 if(i>0){
			 b=true;
		 }
		return b;
	   }
	   
	   /**
	    * 巡检结果添加
	    * @param checktype
	    * @param checkresult
	    * @return
	    */
	   public boolean checkEquipement(String checktype,String checkresult,String id,String isnorfid){
		   boolean b=false;
		   ContentValues values=new ContentValues();
		   values.put("checkstatus", "已检查");
		   values.put("ep_checkdesc", checkresult);
		   values.put("ep_checktype", checktype);
		   values.put("ep_checkdate", DateUtil.getBeforeDay(0));
		   values.put("isupload", "0");
		   values.put("isnorfid", isnorfid);
		  b= super.modify(id, DbTableName.EQUIPEMENT, values);
		return b;
		   
	   }
	   
	   /**
		 * 查询是否拍照
		 * @param waterid
		 * @return
		 */
		public boolean isPhotograph(String waterid){
			boolean b=false;
			if(waterid==null||waterid.equals("")){
				return false;
			}
			String sql="select * from "+DbTableName.RESOURCES+"  where status='5' and type='1' and otherid='"+waterid+"'";
			Cursor cursor=db.rawQuery(sql, null);
			if(cursor.moveToNext()){
				b=true;
			}
			if(cursor!=null){
				cursor.close();
			}
			return false;
			
		}
		
		/**
		 * 巡检时查询设备信息
		 * @param id
		 * @return
		 */
		public OtherEquipmentPOVO queryCheckEquipment(String id,String rfid){
			String sql="select * from otherequipment o where 0=0 ";
			if(id!=null){
				sql=sql+" and _id="+id+" and checkstatus='已检查' or checkstatus='未检查'";
			}
			if(rfid!=null&&!rfid.equals("")){
				sql=sql+" and RFIDnumber='"+rfid+"'";
			}
			OtherEquipmentPOVO e=null;
			Cursor cursor=db.rawQuery(sql, null);
			if(cursor.moveToNext()){
				e=new OtherEquipmentPOVO();
				e.setAdds(cursor.getString(cursor.getColumnIndex("ep_adds")));
				e.setId(cursor.getString(cursor.getColumnIndex("_id")));
				e.setDate(cursor.getString(cursor.getColumnIndex("ep_date")));
				e.setDesc(cursor.getString(cursor.getColumnIndex("ep_dess")));
				e.setLatitude(cursor.getDouble(cursor.getColumnIndex("latitude")));
				e.setLongitude(cursor.getDouble(cursor.getColumnIndex("longitude")));
				e.setLocationadds(cursor.getString(cursor.getColumnIndex("ep_locationadds")));
				e.setNumber(cursor.getString(cursor.getColumnIndex("ep_number")));
				e.setTypeid(cursor.getString(cursor.getColumnIndex("ep_type")));
				e.setTypename(cursor.getString(cursor.getColumnIndex("ep_typename")));
				e.setIsadd(cursor.getString(cursor.getColumnIndex("isadd")));
				e.setRfidNumber(cursor.getString(cursor.getColumnIndex("RFIDnumber")));
				e.setIsupload(cursor.getString(cursor.getColumnIndex("isupload")));
				e.setLastcheckdate(cursor.getString(cursor.getColumnIndex("lastcheckdate")));
				e.setLastcheckresult(cursor.getString(cursor.getColumnIndex("lastcheckresult")));
			}
			if(cursor!=null){
				cursor.close();
			}
			return e;
		}
	   
		/**
		 * 查询未上传的的数据数
		 * @return
		 */
		public int queryNoUploadTotal(String status){
			if(status==null||status.equals("")){
				return 0;
			}
			int i=0;
			String sql="select _id from "+DbTableName.EQUIPEMENT+" where 0=0 ";
			if(status.equals("巡检")){
				sql=sql+"and checkstatus='已检查'" +" and checkupload='0'";
			}else{
				sql=sql+"and isadd='"+status +"' and isupload='0'";
			}
			
			
			Cursor cursor=db.rawQuery(sql, null);
			if(cursor.moveToNext()){
				i=cursor.getCount();
			}
			if(cursor!=null){
				cursor.close();
			}
			return i;
			
		}
		
		/**
		 * 查询应该上传数
		 * @param status
		 * @return
		 */
		public int queryUploadTotal(String status){
			int i=0;
			if(status==null||status.equals("")){
				return i;
			}
			String sql="select _id from "+DbTableName.EQUIPEMENT+" where 0=0 ";
			if(status.equals("巡检")){
				sql=sql+"and checkstatus='已检查'" +" or checkstatus='未检查'";
			}else{
				sql=sql+"and isadd='"+status +"'";
			}
			
			
			Cursor cursor=db.rawQuery(sql, null);
			if(cursor.moveToNext()){
				i=cursor.getCount();
			}
			if(cursor!=null){
				cursor.close();
			}
			return i;
		}
		
		/**
		 * 删除数据
		 */
		public boolean delectAll(){
			boolean b=false;
			db.delete(DbTableName.EQUIPEMENT, "_id>?", new String[]{"0"});
			int id=db.delete(DbTableName.RESOURCES, "status=? or status=?", new String[]{"4","5"});
			db.delete(DbTableName.TYPE, "status", new String[]{"3"});
			if(id>0){
				b=true;
			}
			return b;
			
		};
		
		/**
		 * 上传成功后修改状态
		 * @param id
		 * @param status
		 * @return
		 */
		public boolean modifyStatus(String id,String status){
			boolean b=false;
			ContentValues values=new ContentValues();
			if(status.equals("巡检")){
				values.put("checkupload", "1");
			}else{
				values.put("isupload", "1");
			}
		int k=	db.update(DbTableName.EQUIPEMENT, values, "_id=?", new String[]{id});
		if(k>0){
			b=true;
		}
			return b;
		};
		
		
	
		/**
		 * 设备更换添加
		 * @param e
		 * @return
		 */
		public boolean insert_Replace_Equipement(OtherEquipmentPOVO e){
			boolean b=false;
			return b;
		}
	}
